package com.example.jdai.jdai.ui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.jdai.jdai.MyApplication;
import com.example.jdai.jdai.R;
import com.example.jdai.jdai.config.Config;
import com.example.jdai.jdai.rest.BackRest;
import com.example.jdai.jdai.rest.SendCode;
import com.example.jdai.jdai.util.DataAll;
import com.example.jdai.jdai.util.DataJd;
import com.example.jdai.jdai.util.DataZfb;
import com.example.jdai.jdai.util.Tools;

import java.io.IOException;
import java.util.Map;

import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ZfbActivity extends Activity implements BackRest.DoLong{

    @BindView(R.id.tb)
    TopBar tb;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.activity_bt)
    LinearLayout activityBt;
    @BindString(R.string.no_complete)
    String onComplete;
    ProgressDialog pd;
    @BindString(R.string.sms_tip)
    String smsTip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zfb);
        ButterKnife.bind(this);
        pd=new ProgressDialog(this);
        pd.setCanceledOnTouchOutside(false);
        pd.setMessage(smsTip);
        tb.setTopBarLisener(new TopBar.TopBarLisener() {
            @Override
            public void back() {
                finish();
                saveAll();
            }
            @Override
            public void more() {
                if((!TextUtils.isEmpty(etName.getText().toString().trim()))&&
                        (!TextUtils.isEmpty(etPwd.getText().toString().trim())))
                {
                    pd.show();
                    DataAll.dataZfb.data.put(DataZfb.ZFB_STATUS,"");
                    saveAll();
                    //ZfbOk(ZfbActivity.this);
                    MyApplication.backRest.ZfbOk(ZfbActivity.this);
                    //BackRest.infoUp();
                }
                else {
                    Toast.makeText(ZfbActivity.this,onComplete,Toast.LENGTH_SHORT).show();
                }

                //finish();

            }
        });
        initData();
    }
    void initData()
    {

        String name=DataAll.dataZfb.data.get(DataZfb.ZFB_NAME);
        etName.setText(name);
        etName.setSelection(name.length());
        etPwd.setText(DataAll.dataZfb.data.get(DataZfb.ZFB_PWD));
    }
    boolean saveAll(){
        DataAll.dataZfb.data.put(DataZfb.ZFB_NAME,etName.getText().toString().trim());
        DataAll.dataZfb.data.put(DataZfb.ZFB_PWD,etPwd.getText().toString().trim());
        return DataAll.dataZfb.saveAll();
    }

    @Override
    protected void onDestroy() {
        saveAll();
        finish();
        super.onDestroy();

    }

    @Override
    protected void onPause() {
        super.onPause();
        saveAll();
    }
//    //ok支付宝
//    public  void ZfbOk(final  Activity activity){
//        SendCode send=new SendCode ();
//        send.data.put("user_id", MyApplication.dataAll.data.get(DataAll.LOGIN_ID));
//        send.data.put("zfb_username", DataAll.dataZfb.data.get(DataZfb.ZFB_NAME));
//        send.data.put("zfb_pwd", DataAll.dataZfb.data.get(DataZfb.ZFB_PWD));
//        Call call =MyApplication.okClient.newCall(Tools.getRequest(Config.ZFB_UP_URL,send));
//        call.enqueue(new Callback()
//        {
//            @Override
//            public void onFailure(Call call, IOException e) {
//
//            }
//            @Override
//            public void onResponse(Call call, Response response) throws IOException {
//                int code=response.code();
//                if(200==code){
//                    if (Config.ISDEBUG)
//                        Log.v("zfb",response.body().string());
//                }
//                else if (201==code){
//                    Tools.postToMain(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(MyApplication.applicationContext,"需要验证码",Toast.LENGTH_SHORT).show();
//                            Intent intent=new Intent();
//                            intent.putExtra("plat",1);
//                            startActivity(new Intent(ZfbActivity.this,YzmActivity.class));
//                        }
//                    });
//
//                }
//
//            }
//
//        });
//    }

    @Override
    public void doDone(int mark, Map str) {
        switch (mark){
            case 1:
                Toast.makeText(MyApplication.applicationContext, "支付宝需要验证码", Toast.LENGTH_SHORT).show();
                 Intent intent = new Intent(ZfbActivity.this, YzmActivity.class);
                intent.putExtra("plat", 1);
                intent.putExtra("token", MyApplication.dataAll.data.get(DataAll.TMP));
                startActivity(intent);
                pd.dismiss();
                finish();
                break;
            case 0:
                pd.dismiss();
                startActivity(new Intent(ZfbActivity.this,PhoneActivity.class));
                finish();
                break;
            case 2:
                pd.dismiss();
                Toast.makeText(MyApplication.applicationContext,"密码或账号错误",Toast.LENGTH_SHORT).show();
                break;


        }
    }
}
